package com.steam.org.controller;

import com.steam.basic.util.AjaxResult;
import com.steam.basic.util.PageList;
import com.steam.org.domain.Employee;
import com.steam.org.query.EmployeeQuery;
import com.steam.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门接口类", description = "部门接口类")
@RestController
@RequestMapping("/employee")
class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @ApiOperation(value = "查询单个")
    //根据id查询
    @GetMapping("/{id}")
    public Employee findOne(@PathVariable("id")Long id){
        return employeeService.loadById(id);
    }

    @ApiOperation(value = "查询全部")
    //查询全部
    @GetMapping
    public List<Employee> findAll(){
        return employeeService.loadAll();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id")Long id){
        try {
            employeeService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败!");
        }
    }

    @ApiOperation(value = "添加和修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败!");
        }
    }

    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }

    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败!");
        }
    }

    //根据店铺ID查询店铺人员 = 店长 + 店员
    @GetMapping("/list/{id}")
    public List<Employee> findByShopId(@PathVariable("id") Long shopId){
        return employeeService.findByShopId(shopId);
    }
}
